package cn.wolfcode.wolf2w.redis.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyService;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {
    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public void viewnumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //viewnum + num
        vo.setViewnum(vo.getViewnum() + num);
        //更新vo对象
        this.setStrategyStatisVo(vo);
    }
    @Override
    public void replynumIncrease(String sid, int num) {
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        //评论数+1(num)
        vo.setReplynum(vo.getReplynum() + num);
        //将vo对象设置redis中
        //String vokey = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //template.opsForValue().set(vokey, JSON.toJSONString(vo));
        //更新
        this.setStrategyStatisVo(vo);
    }

    //将vo对象设置redis中
    @Override
    public void setStrategyStatisVo(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        template.opsForValue().set(key, JSON.toJSONString(vo));
    }
    //查询数据
    @Override
    public StrategyStatisVO getStrategyStatisVo(String sid) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sid);
        //判断vo对象是否存在
        StrategyStatisVO vo = null;
        if(template.hasKey(key)){
            //如果存在, 然后viewnum + num
            String voStr = template.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        }else{
            //如果不存在, 先初始化, 然后viewnum + num
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.get(sid);
            //初始化,统计数据(viewnum, replynum....)
            BeanUtils.copyProperties(strategy, vo);
            vo.setStrategyId(strategy.getId());
            template.opsForValue().set(key, JSON.toJSONString(vo));
        }

        return vo;
    }

    //用户攻略收藏操作
    @Override
    public boolean favor(String sid, String uid) {
        //拼接用户攻略收藏id集合的key
        String key = RedisKeys.USER_STRATEGY_FAVOR.join(uid);
        //获取redis上面用户攻略收藏id集合: sidList
        List<String> sidList = new ArrayList<>();
        if(template.hasKey(key)){
            String sidListStr = template.opsForValue().get(key);
            //解析json集合, 参数1:集合json字符串, 参数2:集合中元素类型(泛型)
            sidList = JSON.parseArray(sidListStr, String.class);
        }
        boolean flag = false;
        StrategyStatisVO vo = this.getStrategyStatisVo(sid);
        if(sidList.contains(sid)){
            //判断sid是否在sidList, 如果在表示取消收藏操作, 收藏数-1, 移除出sidList
            vo.setFavornum(vo.getFavornum() -1);
            sidList.remove(sid);
        }else{
            //判断sid是否在sidList, 如果不在表示收藏操作, 收藏数+1, 添加sidList中
            vo.setFavornum(vo.getFavornum() +1);
            sidList.add(sid);
            flag = true;
        }
        //更新list 更新vo
        template.opsForValue().set(key, JSON.toJSONString(sidList));
        this.setStrategyStatisVo(vo);
        //return flag;
        return sidList.contains(sid);
    }

    //用户攻略id收集集合
    @Override
    public List<String> getUserStrategyFavor(String userId) {
        //用户没登录操作
        if (!StringUtils.hasLength(userId)) {
            return Collections.EMPTY_LIST;  //new ArrayList(); 空的集合
        }
        //判断list是否存在,如果存在获取,不存在直接new
        String listkey = RedisKeys.USER_STRATEGY_FAVOR.join(userId);
        List<String> uidList = new ArrayList<>();
        if(template.hasKey(listkey)){
            String sidListStr = template.opsForValue().get(listkey);
            //参数1:json格式字符串  参数2:记号泛型
            uidList = JSON.parseArray(sidListStr, String.class);
        }
        return uidList;
    }

    //用户顶逻辑
    @Override
    public boolean strategyThumbup(String sid, String uid) {
        //构建标记的key
        String key = RedisKeys.STRATEGY_THUMB.join(uid, sid);
        //判断key是否存在
        if(!template.hasKey(key)){
            //如果不存在, 表示今天可以顶, 点赞数+1, 创建标记, 设置有效时间
            StrategyStatisVO vo = this.getStrategyStatisVo(sid);
            vo.setThumbsupnum(vo.getThumbsupnum() + 1);
            //更新
            this.setStrategyStatisVo(vo);

            //有效时间 = 今天最后秒 - 当前秒
            Date now = new Date();
            Date end = DateUtil.getEndDate(now);
            long time = DateUtil.getDateBetween(now, end);
            template.opsForValue().set(key, "1", time, TimeUnit.SECONDS);

            //更新vo对象
            this.setStrategyStatisVo(vo);

            return true;
        }
        //如果存在, 表示今天已经顶过, 直接返回false
        return false;
    }

    @Override
    public boolean isStrategyVoExists(String id) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id);
        return template.hasKey(key);
    }

    @Override
    public List<StrategyStatisVO> queryStrategyVoByPattern(String pattern) {

        List<StrategyStatisVO> list = new ArrayList<>();
        //拼接指定规则
        //strategy_statis_vo:*
        String keyPattern =  RedisKeys.STRATEGY_STATIS_VO.join(pattern);

        //vo的key的集合
        Set<String> keys = template.keys(keyPattern);

        if(keys != null && keys.size() > 0){
            //遍历vo的key集合, 换取 vo对象
            for (String key : keys) {
                String voStr = template.opsForValue().get(key);
                list.add(JSON.parseObject(voStr, StrategyStatisVO.class));
            }
        }
        return list;
    }
}


